package com.rqf.ls.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.rqf.ls.service.IOrderService;
import com.rqf.ls.service.IRegisterOfLawyerService;
import com.rqf.ls.service.IRegisterService;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;

@Controller
@RequestMapping("order")
public class OrderController {

	@Autowired
	private IOrderService orderService;
	@Autowired
	private IRegisterService registerService; 
	@Autowired
	private IRegisterOfLawyerService registerOfLawyerService;
	
	 /**
	 * 增加预约
	 * @param request
	 * @param response
	 * @param phoneNo
	 * @author ServerZhang
	 * @date 2016年3月29日
	 */
	@RequestMapping("add/{phoneNo}")
	public void add(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable String phoneNo){
		String content = request.getParameter("content");
		String fuwusmallclassid = request.getParameter("fuwusmallclassid");
		String whoyuyueaddress = request.getParameter("whoyuyueaddress");
		String yuyuetime = request.getParameter("yuyuetime");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("whoyuyueid", phoneNo);
		map.put("content", content);
		map.put("fuwusmallclassid", fuwusmallclassid);
		map.put("whoyuyueaddress", whoyuyueaddress);
		map.put("yuyuetime", yuyuetime);
		
		Boolean isSuccess = orderService.add(map);
		OutputUtil.returnJsonp(request, response, isSuccess.toString());
	}
	
	 /**
	 * 预约信息获取
	 * @param request
	 * @param response
	 * @author ServerZhang
	 * @date 2016年3月30日
	 */
	@RequestMapping("list")
	public void list(HttpServletRequest request, 
			HttpServletResponse response){
		String phone = request.getParameter("phone");
		String lawyerNo = request.getParameter("lawyerNo");
		DataVector<Map<String, Object>> listData = orderService.getList(phone, lawyerNo, new RequestInput(request));
		List<Map<String, Object>> list = listData.getList();
		Iterator<Map<String, Object>> iterator = list.iterator();
		while(iterator.hasNext()){
			Map<String, Object> next = iterator.next();
			Object userObj = next.get("WHOYUYUEID");
			if (userObj != null) {
				next.put("imgPath", registerService.getPhotoPath(userObj.toString()));
			}
			Object lawyerObj = next.get("YUYUEWHOID");
			if (lawyerObj != null) {
				next.put("imgPath2", registerOfLawyerService.getPhotoPath(lawyerObj.toString()));
			}
		}
		
		String json = JSONSerielizer.toJson(list, null, null);
		response.addHeader("Access-Control-Allow-Origin", "*");
		OutputUtil.returnString(response, json);
	}
	@RequestMapping("form/{orderId}")
	public void form(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable Integer orderId){
		Map<String, Object> form = orderService.getForm(orderId);
		String json = JSONSerielizer.toJson(form, null, null);
		OutputUtil.returnJsonp(request, response, json);
	}
	
	 /**
	 * 律师应约
	 * @param request
	 * @param response
	 * @param lawyerPhone
	 * @author ServerZhang
	 * @date 2016年4月1日
	 */
	@RequestMapping("toOrder/{lawyerPhone}")
	public void toOrder(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable String lawyerPhone){
		String orderId = request.getParameter("orderId");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("yuyuewhoid", lawyerPhone);
		
		Boolean isSuccess = orderService.update(Integer.parseInt(orderId),map);
		OutputUtil.returnJsonp(request, response, isSuccess.toString());
	}
	
}
